﻿using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Kargo
{
    public partial class KargoFatura_DHL : XImportWizard
    {
        public KargoFatura_DHL()
        {
            InitializeComponent();

            Init("Kargo Faturası- DHL", false);

            btnSave.ItemClick += BtnSave_ItemClick;

            this.FileDone += KargoFatura_FileDone;
            this.ProcessStart += KargoFatura_ProcessStart;
            this.ProcessDone += KargoFatura_ProcessDone;
        }

        private void BtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ExecuteNonQuery("InsKargoRaporDhl", dtKargo.Serialize());
            Notify("Aktarım başarılı.");
        }

        void KargoFatura_ProcessStart()
        {
            dtKargo = new DataTable();
            dtKargo.TableName = "Kargo";

            DataSet dsResult = new DataSet();
            dsResult.Tables.Add(dtKargo);

            dtKargo.Columns.Add("WayBillNo", typeof(string));
            dtKargo.Columns.Add("ShipmentDate", typeof(DateTime));
            dtKargo.Columns.Add("Description", typeof(string));
            dtKargo.Columns.Add("InvoiceNo", typeof(string));
            dtKargo.Columns.Add("InvoiceDate", typeof(DateTime));
            dtKargo.Columns.Add("Receiver", typeof(string));
            dtKargo.Columns.Add("Sender", typeof(string));
            dtKargo.Columns.Add("Pieces", typeof(decimal));
            dtKargo.Columns.Add("CurrencyRate", typeof(decimal));
            dtKargo.Columns.Add("InvoiceAmount", typeof(decimal));
            dtKargo.Columns.Add("Weight", typeof(decimal));
        }

        public DataTable dtKargo;

        void KargoFatura_ProcessDone()
        {
            MasterGrid.Init(dtKargo);
            MasterGrid.AddSummary();
            MasterGrid.BestFitColumns();

            btnSave.Enabled = true;
        }

        void KargoFatura_FileDone(DataSet ds, string FileName)
        {
            DataTable dt = ds.Tables[0];    //"KARBEL" isimli çoğu zaman
            DataRowCollection rows = dt.Rows;
            DataColumnCollection cols = dt.Columns;
            Dictionary<string, int> indexes = new Dictionary<string, int>();

            DataRow dr0 = dt.Rows[0];
            for (int colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
            {
                switch (dr0[colIndex].ToString())
                {
                    case "awb no": indexes.Add("WayBillNo", colIndex); break;
                    case "shp dt": indexes.Add("ShipmentDate", colIndex); break;
                    case "shpr ref no": indexes.Add("Description", colIndex); break;
                    case "invce no": indexes.Add("InvoiceNo", colIndex); break;
                    case "invce dt": indexes.Add("InvoiceDate", colIndex); break;
                    case "cnsgnee nm": indexes.Add("Receiver", colIndex); break;
                    case "cnsgnor_nm": indexes.Add("Sender", colIndex); break;
                    case "piece qt": indexes.Add("Pieces", colIndex); break;
                    case "exchange rate": indexes.Add("CurrencyRate", colIndex); break;
                    case "invoice amount eur": indexes.Add("InvoiceAmount", colIndex); break;
                    case "wght qt": indexes.Add("Weight", colIndex); break;
                }
            }

            for (int i = 1; i < dt.Rows.Count; i++)
            {
                DataRow dr = rows[i];
                DataRow drKargo = dtKargo.NewRow();
                foreach (KeyValuePair<string, int> k in indexes)
                    if (k.Key.EndsWith("Date"))
                        drKargo[k.Key] = DateTime.FromOADate(double.Parse(dr[k.Value].ToString())).ToString("dd.MM.yyyy");
                    else
                        drKargo[k.Key] = dr[k.Value];

                dtKargo.Rows.Add(drKargo);
            }

        }

    }
}